package com.yupi.springbootinit.model.dto.autoCommit;

import com.yupi.springbootinit.autocommit.KBEnum;
import com.yupi.springbootinit.autocommit.QuestionEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class AutoCommitResult {
    private List<String> rightFile;
    private List<String> hintFile;
    private List<String> hasCommitFile;
    private List<QuestionDescDto> question;
    private String infos;

    public AutoCommitResult build(List<String> rightFile, List<String> hintFile, List<String> hasCommitFile, KBEnum kbEnum,String message) {
        this.rightFile = rightFile;
        this.hintFile = hintFile;
        this.hasCommitFile = hasCommitFile;
        if (message != null && !message.isEmpty()) {
            this.infos = message;
        }
        Set<String> rightSet = new HashSet<>(rightFile);
        Set<String> hintSet = new HashSet<>(hintFile);
        Set<String> hasCommitSet = new HashSet<>(hasCommitFile);
        List<QuestionEnum> allQuestWithKb = QuestionEnum.getAllQuestWithKb(kbEnum);
        List<QuestionDescDto> questionDescDtos = new ArrayList<>(allQuestWithKb.size());
        for (QuestionEnum questionEnum : allQuestWithKb) {
            QuestionDescDto questionDescDto = new QuestionDescDto();
            questionDescDto.setQuestionDesc(questionEnum.getDesc());
            StringBuilder sb = new StringBuilder();
            List<String> refFile = questionEnum.getRefFile();
            questionDescDto.setResolve(true);
            for (String fileName : refFile) {
                if (!rightSet.contains(fileName)) {
                    questionDescDto.setResolve(false);
                    if (hintSet.contains(fileName)) {
                        sb.append("客户备案文件和2310原始文件不一致：").append(fileName).append("<br>");
                    }
                    if (hasCommitSet.contains(fileName)) {
                        sb.append("文件已在仓库提交过：").append(fileName).append("<br>");
                    }
                }
            }
            if (!questionDescDto.getResolve()) {
                questionDescDto.setResolveDesc(sb.toString());
                questionDescDtos.add(questionDescDto);
            }
        }
        this.question = questionDescDtos;
        return this;
    }
}
